package com.serviceprovider.business.module.order.services.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.order.model.OrderAttribute;
import com.serviceprovider.business.module.order.model.OrderAttributeType;
import com.serviceprovider.business.module.order.services.dao.interfaces.IOrderAttributeDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("orderAttributeDao")
public class OrderAttributeDaoImpl extends JdbcDaoSupport implements IOrderAttributeDao {
    @Autowired
    public OrderAttributeDaoImpl(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    @Override
    public List<OrderAttribute> findAll() {
        String sql = "select * from adm_order_attributes";

        return getJdbcTemplate().query(sql, new OrderAttributeMapper());
    }

    @Override
    public OrderAttribute getById(Integer id) {
        String sql = "select * from adm_order_attributes where id=?";

        return (OrderAttribute) getJdbcTemplate().queryForObject(sql, new Object[] { id }, new OrderAttributeMapper());
    }

    @Override
    public void update(OrderAttribute orderAttribute) {
        String sql = "update adm_order_attributes set name=?, type=?,active=? where id=?";

        getJdbcTemplate().update(sql, new Object[] { orderAttribute.getName(), orderAttribute.getType().toString(),
                orderAttribute.isActive(), orderAttribute.getId() });
        sql = "delete from adm_order_attribute_enum_values where order_attribute_id=?";
        getJdbcTemplate().update(sql, new Object[] { orderAttribute.getId() });
        updateEnumValuesList(orderAttribute);
    }

    private void updateEnumValuesList(OrderAttribute orderAttribute) {
        List<String> list = orderAttribute.getEnumList();

        if (orderAttribute.getType() == OrderAttributeType.LIST) {
            String sql = "insert into adm_order_attribute_enum_values " + "(order_attribute_id,value) values(?,?)";

            for (String item : list) {
                getJdbcTemplate().update(sql, new Object[] { orderAttribute.getId(), item });
            }
        }
    }

    @Override
    public void createNew(OrderAttribute orderAttribute) {
        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()).withTableName(
                                                "adm_order_attributes").usingGeneratedKeyColumns("id");
        Map<String, Object> parameters = new HashMap<String, Object>();

        parameters.put("name", orderAttribute.getName());
        parameters.put("type", orderAttribute.getType().toString());
        parameters.put("active", orderAttribute.isActive());

        Number id = simpleJdbcInsert.executeAndReturnKey(parameters);

        orderAttribute.setId(id.intValue());
        updateEnumValuesList(orderAttribute);
    }

    @Override
    public void removeById(Integer id) {
        String sql = "delete from adm_order_attribute_enum_values where order_attribute_id=?";

        getJdbcTemplate().update(sql, new Object[] { id });
        sql = "delete from adm_order_attributes where id=?";
        getJdbcTemplate().update(sql, new Object[] { id });
    }

    @Override
    public void remove(OrderAttribute orderAttribute) {
        removeById(orderAttribute.getId());
    }

    private class OrderAttributeMapper implements RowMapper {
        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            OrderAttribute result = new OrderAttribute();

            result.setId(resultSet.getInt("id"));
            result.setName(resultSet.getString("name"));
            result.setType(OrderAttributeType.valueOf(resultSet.getString("type")));
            result.setActive(resultSet.getBoolean("active"));

            if (result.getType() == OrderAttributeType.LIST) {
                result.setEnumList(getEnumList(result.getId()));
            }

            return result;
        }

        private List<String> getEnumList(Integer id) {
            String sql = "select value from adm_order_attribute_enum_values where order_attribute_id=?";

            return getJdbcTemplate().query(sql, new Object[] { id }, new RowMapper() {
                @Override
                public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                    return resultSet.getString("value");
                }
            });
        }
    }
}



